package com.blast.manager.pop

import android.content.Context
import android.view.KeyEvent
import android.view.View
import androidx.appcompat.widget.AppCompatButton
import com.blast.manager.R
import com.blast.manager.delay.event.KeyDownEvent
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.AttachPopupView
import com.lxj.xpopup.enums.PopupAnimation
import com.wxl.common.base.AbsActivity
import com.wxl.common.life.LifecycleManager
import com.wxl.common.util.ObservableUtil
import kotlinx.android.synthetic.main.pop_menu_more.view.*

class DelayMorePop : AttachPopupView {

    lateinit var onClick: ObservableUtil.Companion.OnNextCallback<Int>

    private var index = 0
    private val btns = arrayListOf<AppCompatButton>()

    constructor(context: Context) : super(context)

    override fun getImplLayoutId(): Int {
        return R.layout.pop_menu_more
    }

    override fun onCreate() {
        super.onCreate()
        val mKeyDownEvent = KeyDownEvent()
        mKeyDownEvent.isCanBack = false
        LifecycleManager.manager.refreshLiveData(mKeyDownEvent)
        morePopUpdateBtn.setOnClickListener {
            dismiss()
            onClick.onNext(0)
        }
        morePopOnlineBtn.setOnClickListener {
            dismiss()
            onClick.onNext(1)
        }

        morePopClearBtn.setOnClickListener {
            dismiss()
            onClick.onNext(2)
        }

        morePopDeleteBtn.setOnClickListener {
            dismiss()
            onClick.onNext(3)
        }
        btns.add(morePopUpdateBtn)
        btns.add(morePopOnlineBtn)
        btns.add(morePopClearBtn)
        btns.add(morePopDeleteBtn)

        morePopUpdateBtn.isSelected = true
        morePopUpdateBtn.isFocusable = true
        btns[index].requestFocus()

    }

    private fun setOnClickCallback(onClick: ObservableUtil.Companion.OnNextCallback<Int>): DelayMorePop {
        this.onClick = onClick
        return this
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
            if (index < btns.size - 1) {
                btns[index].isSelected = false
                btns[index].isFocusable = false
                ++index
                btns[index].isSelected = true
                btns[index].isFocusable = true
                btns[index].requestFocus()
            }
        }
        if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
            if (index > 0 && btns.isNotEmpty()) {
                btns[index].isSelected = false
                btns[index].isFocusable = false
                --index
                btns[index].isSelected = true
                btns[index].isFocusable = true
                btns[index].requestFocus()
            }
        }
        if(keyCode == KeyEvent.KEYCODE_BACK){
            dismiss()
        }
        return true
    }

    private fun release(){
        btns.clear()
        val mKeyDownEvent = KeyDownEvent()
        mKeyDownEvent.isCanBack = true
        LifecycleManager.manager.refreshLiveData(mKeyDownEvent)
    }

    override fun onDismiss() {
        super.onDismiss()
        release()
    }


    companion object {
        fun show(atView: View, onClick: ObservableUtil.Companion.OnNextCallback<Int>) :DelayMorePop {
            val pop = DelayMorePop(atView.context).setOnClickCallback(onClick)
            XPopup.Builder(atView.context)
                .dismissOnTouchOutside(true)
                .dismissOnBackPressed(true)
                .hasShadowBg(false)
                .isRequestFocus(false)
                .popupAnimation(PopupAnimation.ScrollAlphaFromBottom)
                .atView(atView)
                .asCustom(pop)
                .show()
            (atView.context as AbsActivity).addPop(pop)
            return pop
        }
    }
}